<!-- 编辑弹窗 -->
<template>
    <ele-modal
      width="30%"
      :movable="false"
      :visible="visible"
      :close-on-click-modal="true"
      custom-class="ele-dialog-form"
      :title="isUpdate ? '修改银行日志账信息' : '添加银行日志账信息'"
      @update:visible="updateVisible" 
    >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
        <el-card  >        
          <el-row :gutter="24">
            <el-col :md="24" :sm="24">
                <el-form-item label="业务员:" prop="saleId">
                  <el-select v-model="form.saleId" clearable placeholder="请选择业务员" style="width:100%" @change="changeSale">
                    <el-option v-for="(item, index) in saleDatas" :key="index" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="发生日期:"  prop="peddingDate">
                <el-date-picker v-model="form.peddingDate" type="date" placeholder="请输入发生日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width:100%"/>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="银行账号:" prop="bankaccountCode">
                <el-input v-model="form.bankaccountCode" placeholder="请输入银行账号"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="收入:" prop="income">
                <el-input v-model="form.income" placeholder="请输入收入"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="支出:" prop="expense">
                <el-input v-model="form.expense" placeholder="请输入支出"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="用途:" prop="useDesc">
                <el-input v-model="form.useDesc" placeholder="请输入用途"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="是否已认领:" >
                <el-input v-model="form.isClaim" placeholder="请输入是否已认领"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="备注:">
                <el-input v-model="form.remark" placeholder="请输入备注"  :rows="4" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save" v-permission="'Financial_BackLog_Save'" >
        保存
      </el-button>
    </div>
    </ele-modal>
  </template>
  <script>

  //初始化对像
  import { GetAllAccount } from '@/api/usermanager/accountmanager';
  import { UpdateBankstatement,GetBankstatementById } from '@/api/finance/banklog';
  import { isIntegerOrDecimal2,validatePhoneTwo,isIntegerNotMust } from '@/utils/validate';
  export default {
    name: 'BankLogEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
    },
    data() {
      return {
        // 表单数据
        form: {},
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        saleDatas:[],
        // 表单验证规则
        rules: {
          costTypeId: [{required: true,message: '请输入费用类型',trigger: 'blur'}],
          amount: [{required: true,message: '请输入金额',trigger: 'blur'}],
          peddingDate: [{required: true,message: '请输入发生日期',trigger: 'blur'}],
          bankAccountId: [{required: true,message: '请输入支出账户',trigger: 'blur'}],
          income: [{validator:isIntegerOrDecimal2,trigger:'blur'}],
          expense: [{validator:isIntegerOrDecimal2,trigger:'blur'}],
        },
      };
    },
    created(){
      this.initData();
    },
    methods: {

      /* 初始化数量 */
      initData(){

        GetAllAccount().then((data)=>{
          this.saleDatas = [];
          this.saleDatas = data;
        });

      },

      /* 保存编辑 */
      save() {
        //校验必填信息
        this.$refs['form'].validate((valid) => {
          if (!valid) {return false;}

          this.loading = true;
          const saveOrUpdate = this.isUpdate ? UpdateBankstatement : UpdateBankstatement;
          saveOrUpdate(this.form)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              //回调父组件方法
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },

      setData(id){
        GetBankstatementById(id)
          .then((res) => {
            this.form = res; 
        });
      },

      changeSale(val){
       let saler =  this.saleDatas.find((d)=>{
          return d.id == val;
        });
        if(saler){
          this.form.saleName = saler.name;
        }
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        //更新文件列表对像
      },
      
  },
    
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data && this.data.id) {
            //赋值this.form
            this.form = {};
            this.setData(this.data.id);
            this.isUpdate = this.data.id !=null && this.data.id !=undefined;
          } else {
            this.form={};
            this.isUpdate = false;
          }
        } else {
            this.$refs['form'].clearValidate();
            this.form = {};
        }
      }
    }
  };
  </script>